package javaapplication1;

import com.jpackages.jtray.BalloonType;
import com.jpackages.jtray.IconAnimation;
import com.jpackages.jtray.JTrayIcon;
import com.jpackages.jtray.JTrayLibraryLoadFailedException;
import com.jpackages.jtray.JTrayListener;
import com.web.server.WEB920Server;
import com.web.server.common.exception.Web920Exception;
import com.web.server.model.test.TestAction;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.event.ChangeEvent;


/**
 * Example code using JTray
 * Copyright (c) 2001-2007
 * Company: VersaEdge Software, LLC
 */

public class Example2 extends JFrame implements JTrayListener {
  // handle to the JTrayIcon to display
  JTrayIcon jTrayIcon;

  // define popup menu components
  JPopupMenu jPopupMenu = new JPopupMenu();
  JMenuItem jMenuItemHideFrame = new JMenuItem();
  JMenuItem jMenuItemShowFrame = new JMenuItem();
  JCheckBoxMenuItem jCheckBoxMenuItemAnimation = new JCheckBoxMenuItem();
  JMenuItem jMenuItemClose = new JMenuItem();
  JSlider animSlider = new JSlider();
  JMenu menuLookAndFeel = new JMenu("Look & Feel");
  JRadioButtonMenuItem jRadioButtonMenuItemWindows = new JRadioButtonMenuItem();
  JRadioButtonMenuItem jRadioButtonMenuItemMetal = new JRadioButtonMenuItem();
  JMenuItem jMenuItemAbout = new JMenuItem();
  JMenuItem jMenuItemBalloon = new JMenuItem();

  // define a tabbed pane and its components
  JTabbedPane jTabbedPane = new JTabbedPane();
  JPanel panelTab1 = new JPanel();
  JPanel panelTab2 = new JPanel();
  BorderLayout borderLayout2 = new BorderLayout();
  JScrollPane jScrollPane2 = new JScrollPane();
  JPanel jPanel1 = new JPanel();
  JTextArea jTextAreaMessage = new JTextArea();
  JButton jButtonSend = new JButton("Send");
  JButton jButtonClose = new JButton("Close");
  JLabel jLabel1 = new JLabel();  
  
  static Image loadImage(String path) {
	  return (new ImageIcon(path)).getImage();
  }
  
  // define some images  
  Image imageHost = loadImage("image/Host24.gif");
  Image imageAbout = loadImage("image/About24.gif");
  Image imageStop = loadImage("image/Stop24.gif");

  // define Example2 Frame GUI elements
  BorderLayout borderLayout1 = new BorderLayout();
  JScrollPane jScrollPane1 = new JScrollPane();
  JTextArea jTextArea1 = new JTextArea();

  public Example2() {
    // initialize GUI components
    try {
      jbInit();
    }
    catch(Exception e) {      
    }

    try {
      // create an instance of a JTrayIcon which uses image\ani_01.gif as the icon to display
      Image i1 = loadImage("image/ani_01.gif");     
      jTrayIcon = new JTrayIcon(i1);
      // specify the tool tip text
      jTrayIcon.setToolTipText("JTray Example2");
      // set this as the JTrayListener
      jTrayIcon.addJTrayListener(this);


      // set the right click target to the popup menu after the jPopupMenu and its components have been completely defined
      jTrayIcon.setRightClickTarget(jPopupMenu);

      // set the left click target to the tabbed pane after it and its components have been completely defined
      jTrayIcon.setLeftClickTarget(this.jTabbedPane);

      // the following addNoHideComonent methods only work properly in JRE 1.4 or later
      // set the tabbed pane as a no hide component so it can be clicked without making it disappear
      jTrayIcon.addNoHideComponent(jTabbedPane);
      // set the message text area as a no hide component so it can be clicked on and used to accept text
      jTrayIcon.addNoHideComponent(jTextAreaMessage);
      // add the scroll bars of the scroll pane with the text area so that they can be used to scroll without making the target disappear
      jTrayIcon.addNoHideComponent(jScrollPane2.getHorizontalScrollBar());
      jTrayIcon.addNoHideComponent(jScrollPane2.getVerticalScrollBar());

      // define an animation
      IconAnimation iconAnimation = new IconAnimation();      
      iconAnimation.addIcon(loadImage("image/ani_01.gif"));
      iconAnimation.addIcon(loadImage("image/ani_02.gif"));
      iconAnimation.addIcon(loadImage("image/ani_03.gif"));
      iconAnimation.addIcon(loadImage("image/ani_04.gif"));
      iconAnimation.addIcon(loadImage("image/ani_05.gif"));
      iconAnimation.addIcon(loadImage("image/ani_06.gif"));
      iconAnimation.addIcon(loadImage("image/ani_07.gif"));
      iconAnimation.addIcon(loadImage("image/ani_08.gif"));
           
      jTrayIcon.setAnimation(iconAnimation);

      // display the JTrayIcon
      jTrayIcon.show();
    }
    catch (JTrayLibraryLoadFailedException e) {
      System.out.println(e);
      System.exit(0);
    }
  }

  //Main Method
  public static void main(String[] args) throws Web920Exception {
     Example2 frame = new Example2();
//  if (null == args || args.length == 0) {
//            args = new String[]{"920web.com"};
//        }
//
//        // 判断是否提供了参数，如果有就启动模块，是否直接退出
//        if (null != args && args.length > 0) {
//
//            WEB920Server.startServer(args[0]);
//        } else {
//            System.out.println("--------------------");
//            System.exit(0);
//            // 记录日志
//            //LogUtil.getDebugLogger().debug("缺少系统启动参数");
//            Web920Exception ex = new Web920Exception("系统启动失败：缺少系统启动参数");
//            //LogUtil.saveRunLog(ex);
//            throw ex;
//        }
    //Center the window
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension frameSize = frame.getSize();
    if (frameSize.height > screenSize.height) {
      frameSize.height = screenSize.height;
    }
    if (frameSize.width > screenSize.width) {
      frameSize.width = screenSize.width;
    }
    frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);

    frame.setVisible(false);
  }

  //GUI initialization
  private void jbInit() throws Exception {
    this.setTitle("JTray Example2");
    this.addWindowListener(new java.awt.event.WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        this_windowClosing(e);
      }
    });
    this.setSize(300, 200);
    this.getContentPane().setLayout(borderLayout1);
    jTextArea1.setEnabled(false);
    jTextArea1.setDisabledTextColor(Color.darkGray);
    String textMessage = "An icon has been added to the system tray.  Right and left click on it to see example functionality.\n\n"
                       + "If using the trial edition of JTray, an additional demo icon is also displayed.";
    jTextArea1.setText(textMessage);
    jTextArea1.setLineWrap(true);
    jTextArea1.setWrapStyleWord(true);
    jTabbedPane.setPreferredSize(new Dimension(250, 200));
    jTextAreaMessage.setText("Type a message");
    jPanel1.setPreferredSize(new Dimension(10, 30));
    jButtonSend.setPreferredSize(new Dimension(79, 24));
    jButtonClose.setPreferredSize(new Dimension(79, 24));
    jLabel1.setIcon(new ImageIcon("example\\image\\pic.gif"));
    panelTab1.setLayout(borderLayout2);
    jMenuItemBalloon.setText("Show Balloon");
    jMenuItemBalloon.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jMenuItemBalloon_actionPerformed(e);
      }
    });
    panelTab1.add(jScrollPane2,  BorderLayout.CENTER);
    jScrollPane2.getViewport().add(jTextAreaMessage, null);
    panelTab1.add(jPanel1,  BorderLayout.SOUTH);
    jTabbedPane.addTab("Message", panelTab1);
    jTabbedPane.addTab("Picture", panelTab2);
    panelTab2.add(jLabel1, null);
    jMenuItemHideFrame.setText("Hide Frame");
    jMenuItemHideFrame.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jMenuItemHideFrame_actionPerformed(e);
      }
    });
    jMenuItemShowFrame.setText("Show Frame");
    jMenuItemShowFrame.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jMenuItemShowFrame_actionPerformed(e);
      }
    });
    jMenuItemClose.setText("Close");
    jMenuItemClose.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jMenuItemClose_actionPerformed(e);
      }
    });
    jCheckBoxMenuItemAnimation.setText("Animation");
    jCheckBoxMenuItemAnimation.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jCheckBoxMenuItemAnimation_actionPerformed(e);
      }
    });
    animSlider.setPreferredSize(new Dimension(50, 24));
    animSlider.addChangeListener(new javax.swing.event.ChangeListener() {
      public void stateChanged(ChangeEvent e) {
        animSlider_stateChanged(e);
      }
    });
    ButtonGroup buttonGroup = new ButtonGroup();
    jRadioButtonMenuItemMetal.setSelected(true);
    jMenuItemAbout.setText("About");
    jMenuItemAbout.setIcon(new ImageIcon(imageAbout));
    jMenuItemAbout.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jMenuItemAbout_actionPerformed(e);
      }
    });
    buttonGroup.add(jRadioButtonMenuItemWindows);
    buttonGroup.add(jRadioButtonMenuItemMetal);
    jRadioButtonMenuItemWindows.setText("Windows");
    jRadioButtonMenuItemWindows.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jRadioButtonMenuItemWindows_actionPerformed(e);
      }
    });
    jRadioButtonMenuItemMetal.setText("Metal");
    jRadioButtonMenuItemMetal.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        jRadioButtonMenuItemMetal_actionPerformed(e);
      }
    });
    animSlider.setMinimum(30);
    animSlider.setMaximum(200);
    animSlider.setValue(130);
    jMenuItemClose.setIcon(new ImageIcon(imageStop));
    this.getContentPane().add(jScrollPane1,  BorderLayout.CENTER);
    jScrollPane1.getViewport().add(jTextArea1, null);

    jPopupMenu.add(jMenuItemHideFrame);
    jPopupMenu.add(jMenuItemShowFrame);
    jPopupMenu.add(jMenuItemBalloon);
    jPopupMenu.addSeparator();
    jPopupMenu.add(jCheckBoxMenuItemAnimation);
    jPopupMenu.add(animSlider);
    jPopupMenu.addSeparator();
    menuLookAndFeel.add(jRadioButtonMenuItemMetal);
    menuLookAndFeel.add(jRadioButtonMenuItemWindows);
    menuLookAndFeel.setIcon(new ImageIcon(imageHost));
    jPopupMenu.add(menuLookAndFeel);
    jPopupMenu.add(jMenuItemAbout);
    jPopupMenu.add(jMenuItemClose);
    jPanel1.add(jButtonSend, null);
    jPanel1.add(jButtonClose, null);
  }

  // this method is called when the application should close
  void closeApp() {
    // check if null since it could be null if a JTrayLibraryLoadFailedException is thrown
    if (jTrayIcon != null) {
      // no longer display this icon
      jTrayIcon.hide();
    }
    System.exit(0);
  }

  void jMenuItemHideFrame_actionPerformed(ActionEvent e) {
    this.setVisible(false);
  }

  void jMenuItemShowFrame_actionPerformed(ActionEvent e) {
    this.setVisible(true);
  }

  void jMenuItemClose_actionPerformed(ActionEvent e) {
    closeApp();
  }

  void this_windowClosing(WindowEvent e) {
    //closeApp();
      this.setVisible(false);
  }

  void jCheckBoxMenuItemAnimation_actionPerformed(ActionEvent e) {
    if (jTrayIcon == null)
      return;

    // if it is animating, stop the animation
    if (jTrayIcon.isAnimating()) {
      jTrayIcon.stopAnimation();
      // reset to the default icon shown
      jTrayIcon.resetIcon();
    }
    // otherwise, start the animation
    else {
      jTrayIcon.startAnimation();
    }
  }

  // when the animation slider is changed, adjust the animation frame delay appropriately
  // moving the slider right makes the delay lower which increases the animation speed
  void animSlider_stateChanged(ChangeEvent e) {
    if (jTrayIcon != null) {
      int delay = 230 - animSlider.getValue();
      jTrayIcon.setAnimationFrameDelay(delay);
    }
  }

  // set the look and feel to metal
  void jRadioButtonMenuItemMetal_actionPerformed(ActionEvent e) {
    String className = "";
    // find the metal look and feel class name
    UIManager.LookAndFeelInfo lf[] = UIManager.getInstalledLookAndFeels();
    for (int i=0; i < lf.length; i++) {
      if (lf[i].getClassName().toLowerCase().indexOf("metal") > 0) {
        className = lf[i].getClassName();
        break;
      }
    }

    // set the look and feel
    try {
      UIManager.setLookAndFeel(className);
      SwingUtilities.updateComponentTreeUI(this);
      SwingUtilities.updateComponentTreeUI(jTabbedPane);
      SwingUtilities.updateComponentTreeUI(jPopupMenu);
    } catch (Exception ex) {}
  }

  // set the look and feel to windows
  void jRadioButtonMenuItemWindows_actionPerformed(ActionEvent e) {
    String className = "";
    // find the windows look and feel class name
    UIManager.LookAndFeelInfo lf[] = UIManager.getInstalledLookAndFeels();
    for (int i=0; i < lf.length; i++) {
      if (lf[i].getClassName().toLowerCase().indexOf("windows") > 0) {
        className = lf[i].getClassName();
        break;
      }
    }

    // set the look and feel
    try {
      UIManager.setLookAndFeel(className);
      SwingUtilities.updateComponentTreeUI(this);
      SwingUtilities.updateComponentTreeUI(jTabbedPane);
      SwingUtilities.updateComponentTreeUI(jPopupMenu);
    } catch (Exception ex) {}
  }

  // display About info
  void jMenuItemAbout_actionPerformed(ActionEvent e) {
    // need to disable targets before showing a modal dialog box
    jTrayIcon.disableTargets();
    JOptionPane.showMessageDialog(null, "JTray is a VersaEdge Software, LLC product.  Visit jpackages.com for more information.");
    // enable targets after showing a modal dialog box
    jTrayIcon.enableTargets();
  }

  // defined for the JTrayListener
  public void taskbarCreated() {
    System.out.println("taskbarCreated");
  }

  // defined for the JTrayListener
  public void mouseClickRight(JTrayIcon icon, Point p) {
    System.out.println("right click @ " + p.x + ", " + p.y);
  }

  // defined for the JTrayListener
  public void mouseClickLeft(JTrayIcon icon, Point p) {
    System.out.println("left click @ " + p.x + ", " + p.y);
    //WEB920Server.stopServer();
    TestAction test = new TestAction();
    test.test();
  }

  // defined for the JTrayListener
  public void mouseDoubleClickLeft(JTrayIcon icon, Point p) {
    System.out.println("left double click @ " + p.x + ", " + p.y);
    //WEB920Server.startServer(null);
  }
  
//defined for the JTrayListener
  public void mouseDoubleClickRight(JTrayIcon icon, Point p) {
    System.out.println("right double click @ " + p.x + ", " + p.y);
  }

  void jMenuItemBalloon_actionPerformed(ActionEvent e) {
    if (jTrayIcon == null)
      return;

    jTrayIcon.showBalloonMessage("JTray API", "This is a balloon message", BalloonType.ERROR);
  }


}